<template>
  <div class="blind-box-records">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>盲盒抽奖记录</span>
        </div>
      </template>

      <!-- 搜索栏 -->
      <el-form :inline="true" class="search-form">
        <el-form-item label="盲盒名称">
          <el-input v-model="searchForm.blind_box_name" placeholder="请输入盲盒名称" clearable @clear="handleSearch" />
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="searchForm.customer_name" placeholder="请输入用户名" clearable @clear="handleSearch" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 表格 -->
      <el-table :data="tableData" border stripe v-loading="loading">
        <el-table-column prop="id" label="记录ID" width="80" />
        <el-table-column prop="blind_box_name" label="盲盒名称" min-width="150" />
        <el-table-column prop="customer_name" label="用户" width="120" />
        <el-table-column prop="telephone" label="手机号" width="120" />
        <el-table-column label="奖品图片" width="100">
          <template #default="scope">
            <el-image 
              :src="scope.row.prize_image" 
              fit="cover"
              style="width: 50px; height: 50px;"
            />
          </template>
        </el-table-column>
        <el-table-column prop="prize_name" label="奖品名称" min-width="150" />
        <el-table-column prop="prize_level" label="等级" width="80" />
        <el-table-column label="是否发放" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.is_delivered === 1 ? 'success' : 'warning'">
              {{ scope.row.is_delivered === 1 ? '已发放' : '未发放' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="抽取时间" width="180" />
        <el-table-column label="操作" width="120">
          <template #default="scope">
            <el-button 
              v-if="scope.row.is_delivered === 0"
              size="small" 
              type="primary"
              @click="handleDeliver(scope.row)"
            >
              发放
            </el-button>
            <span v-else style="color: #67c23a;">已发放</span>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @current-change="handlePageChange"
        :current-page="currentPage"
        :page-size="pageSize"
        layout="total, prev, pager, next"
        :total="total"
        style="margin-top: 20px; text-align: right;"
      />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import axios from 'axios'
import { ElMessage, ElMessageBox } from 'element-plus'

const VITE_URL = (window as any).VITE_URL

const loading = ref(false)
const tableData = ref([])
const searchForm = ref({
  blind_box_name: '',
  customer_name: ''
})

const currentPage = ref(1)
const pageSize = ref(15)
const total = ref(0)

// 获取抽奖记录列表
const getList = async () => {
  loading.value = true
  try {
    const params = new URLSearchParams()
    params.append('page', currentPage.value.toString())
    params.append('per_page', pageSize.value.toString())
    
    // 注意：后端接口使用 blind_box_id 而不是 blind_box_name
    if (searchForm.value.blind_box_name) {
      params.append('blind_box_name', searchForm.value.blind_box_name)
    }
    if (searchForm.value.customer_name) {
      params.append('customer_name', searchForm.value.customer_name)
    }

    const url = `${VITE_URL}/admin.blindBox/get_records?${params.toString()}`
    const res = await axios.get(url)
    tableData.value = res.data.list || res.data
    total.value = res.data.total || res.data.length
  } catch (error) {
    ElMessage.error('获取列表失败')
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = () => {
  currentPage.value = 1
  getList()
}

// 重置
const handleReset = () => {
  searchForm.value = {
    blind_box_name: '',
    customer_name: ''
  }
  currentPage.value = 1
  getList()
}

// 分页
const handlePageChange = (page: number) => {
  currentPage.value = page
  getList()
}

// 发放奖品
const handleDeliver = async (row: any) => {
  try {
    await ElMessageBox.confirm('确定要发放该奖品吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })

    const url = `${VITE_URL}/admin.blindBox/deliver_prize`
    const formData = new FormData()
    formData.append('record_id', row.id.toString())

    const res = await axios.post(url, formData)
    if (res.data.status === 0) {
      ElMessage.success('发放成功')
      getList()
    } else {
      ElMessage.error(res.data.message || '发放失败')
    }
  } catch (error) {
    // 用户取消
  }
}

onMounted(() => {
  getList()
})
</script>

<style scoped>
.blind-box-records {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-form {
  margin-bottom: 20px;
}
</style>